package eop.graham

class DataController {
	LoadDataFromTsvFileService loadDataFromTsvFile

	//def index = {render (view:"importdata")}

	def importdata = {
		//println "Saw filename ${params.dataFile} and researcher ${params.researcher}"
		//TODO need to check researcher is in Experiment.RESEARCH_TEAM

		def startTime = System.currentTimeMillis()
		LoadDataFromTsvFileService x = new LoadDataFromTsvFileService()
		// check file exists before trying to load data
		def dFile = new File("${params.dataFile}")
		def errorLines = []

		if (dFile.exists() && dFile.canRead()){
			try {
				errorLines = x.loadDataFromTsvFile(params.dataFile)
			}catch(Exception e)
			{
				return [status:"Error processing file: ${e.message}"]
			}
			def elapsedTime = (System.currentTimeMillis()-startTime)/1000
			def errorTotal = errorLines.size
			String errorString = (errorTotal > 0)?"$errorTotal errors in lines ${errorLines}":"no errors detected"
		return [status: "Processed file ${params.dataFile} in ${elapsedTime} seconds with ${errorString}"]
		}
	else {
		return [status:"Could not read file ${params.dataFile}"]
	}
}

def scaffold = Data

//def list = {}

}
